const express = require('express');
const router = express.Router();
let db = require("../db.js");
let dbP = require("../db_promise");
const utility = require('utility');

// 获取个人信息
router.get("/userinfo", function(req, res) {
  // 前端：没有id参数
  //       传token:(id+私有字符串)  中间件解密 req.user
  let id = req.user.id;

  // 后台：查数据库用id;
  db("select * from user where id=?", id, function(err, arr) {
    if (err != null) {
      res.send({
        status: 1,
        message: "获取个人信息失败！",
      });
    } else {
      res.send({
        status: 0,
        message: "获取个人信息成功！",
        data: arr[0]
      });
    }
  });



});


// 修改个人信息
router.post("/userinfo", function(req, res) {
  // 1.获取 req.body;  POST数据

  // 2.修改数据
  db("update user set ? where id=?", [req.body, req.body.id], function(err, data) {
    if (err != null) {
      res.send({
        status: 1,
        message: "修改个人信息失败！",
      });
    } else {
      res.send({
        status: 0,
        message: "修改个人信息成功！",
      });
    }
  });

});


// 修改密码
router.post("/updatepwd", function(req, res) {
  // 1.req.body 拿到数据 req.body
  let oldPwd = utility.md5(req.body.oldPwd);
  let newPwd = utility.md5(req.body.newPwd);



  // 2.修改行么？
  // 找有没有这么个  id  and oldPwd
  db("select * from user where id=? and password=?", [req.user.id, oldPwd], function(err, arr) {
    // 查询有问题
    if (err != null) {
      res.send({
        status: 1,
        message: "查询数据库有误！",
      });
    }
    // 查询没有问题
    else {
      // 查无此人
      if (arr.length == 0) {
        res.send({
          status: 1,
          message: "旧密码输入错误！",
        });
      }
      // 查到有这个人：修改
      else {

        db("update user set password=? where id=?", [newPwd, req.user.id], function(err, data) {
          if (err != null) {
            res.send({
              status: 1,
              message: "修改个人密码失败！",
            });
          } else {
            res.send({
              status: 0,
              message: "修改个人密码成功！",
            });
          }
        })

      }
    }
  });

});
// 优化接口：了解
router.post("/updatepwdpromise", async function(req, res) {
  // 1.req.body 拿到数据 req.body
  let oldPwd = utility.md5(req.body.oldPwd);
  let newPwd = utility.md5(req.body.newPwd);

  // 2.查询 id  oldPwd;
  try {
    let arr = await dbP("select * from user where id=? and password=?", [req.user.id, oldPwd]);
    if (arr.length != 0) {
      let obj = await dbP("update user set password=? where id=?", [newPwd, req.user.id]);
      res.send({
        status: 0,
        message: "修改密码成功！",
      });
    }
  } catch (error) {
    res.send({
      status: 1,
      message: "修改密码失败！",
    });
  }



});



// 修改头像
router.post("/update/avatar", function(req, res) {
  // 1. req.body
  db("update user set user_pic=? where id=?", [req.body.avatar, req.user.id], function(err, data) {
    if (err != null) {
      res.send({
        status: 1,
        message: "修改个人头像失败！",
      });
    } else {
      res.send({
        status: 0,
        message: "修改个人头像成功！",
      });
    }
  });

});


// 修改头像的优化：了解
router.post("/update/avatar1", async function(req, res) {
  // 1. req.body
  try {
    await dbP("update user set user_pic=? where id=?", [req.body.avatar, req.user.id]);
    res.send({
      status: 0,
      message: "修改个人头像成功！",
    });
  } catch (error) {
    res.send({
      status: 1,
      message: "修改个人头像失败！",
    });
  }
});

module.exports = router;